<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>店铺管理</title>
<meta name="description" content="拉勾数据">
<meta name="keywords" content="二手房 房产经济 房企 开发商 拉勾数据 IFS">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="icon" type="image/png" href="assets/i/favicon.png">
<link rel="apple-touch-icon-precomposed"
	href="assets/i/app-icon72x72@2x.png">
<meta name="apple-mobile-web-app-title" content="eosms for IFS" />
<link rel="stylesheet" href="assets/css/amazeui.min.css" />
<link rel="stylesheet" href="assets/css/admin.css">
</head>
<body>
	<!--[if lte IE 9]>
<p class="browsehappy">你正在使用<strong>过时</strong>的浏览器，Amaze UI 暂不支持。 请 <a href="http://browsehappy.com/" target="_blank">升级浏览器</a>
  以获得更好的体验！</p>
<![endif]-->

	<jsp:include page="/manage_header.do" flush="true" />

	<div class="am-cf admin-main">
		<!-- sidebar start -->
		<jsp:include page="/manage_tree.do" flush="true" ></jsp:include>
		<!-- sidebar end -->

		<!-- content start -->
		<div class="admin-content">

			<div class="am-cf am-padding">
				<div class="am-fl am-cf">
					<strong class="am-text-primary am-text-lg">店铺</strong> / <small>store</small>
				</div>
			</div>
			<input type="hidden" id="province1" name="province1" value="${user.province.code }">
			<input type="hidden" id="city1" name="city1" value="${user.city.code }">
			<input type="hidden" id="county1" name="county1" value="${user.county.code }">
			<form id="store_form" class="am-form am-form-horizontal" action="show_add_store.do"
				method="post">
				<input type="hidden" name="id" id="id" value="0"> <input
					type="hidden" name="page_num" id="page_num" value="0">
				<div class="am-tabs am-margin" data-am-tabs>
					<ul class="am-tabs-nav am-nav am-nav-tabs">
						<li class="am-active"><a href="#tab1">基本信息</a></li>
					</ul>


					<div class="am-tabs-bd">
						<div class="am-tab-panel am-fade am-in am-active" id="tab1">
							<div class="am-g am-margin-top">
							  <div class="am-form-group">
								<label for="user-name" class="am-u-sm-3 am-form-label">店铺名称</label>
								<div class="am-u-sm-9 am-u-md-9">
								<input type="text" class="am-input-sm" name="shopname" id="shopname"
										value="${shopname}">
								</div>
								</div>
								  <div class="am-form-group">
									<label for="user-name" class="am-u-sm-3 am-form-label">店长姓名</label>
								<div class="am-u-sm-9 am-u-md-9">
								<input type="text" class="am-input-sm" name="shopmanagename" id="shopmanagename"
										value="${shopmanagename}">
								</div>
								</div>
								  <div class="am-form-group">
								<label for="user-name" class="am-u-sm-3 am-form-label">电话</label>
								<div class="am-u-sm-9 am-u-md-9">
									<input type="number" class="am-input-sm" name="phone" id="phone"
										value="${phone}">
								</div>
								</div>
								  <div class="am-form-group">
								<label for="user-name" class="am-u-sm-3 am-form-label">经营类别</label>
								<div class="am-u-sm-9 am-u-md-9">
									<input type="text" class="am-input-sm" name="type" id="type"
										value="${type}">
								</div>
								</div>
								  <div class="am-form-group">
									<label for="user-name" class="am-u-sm-3 am-form-label">上传营业执照</label>
								  <div class="am-u-sm-6 am-u-lg-6 am-text-left">
									<input type="text" name="filetext" id="filetext"  readonly="true" style="height:30px">
							        </div>
							
									<div class="am-u-sm-3 am-u-lg-3 am-text-left">
									<input type='button' value='浏览'  OnClick="file.click();" class="am-btn am-btn-primary am-btn-xs"/>
									<input type="file" name="file" id="file" onChange="geturl(this.value)"> <br>
								</div>
								 </div>
								  <div class="am-form-group" id="imgdiv">
								<label for="user-name" class="am-u-sm-3 am-form-label"></label>
								<div class="am-u-sm-9 am-u-md-9">
									   <img id="img1" height="90" width="180" />
								</div>
								</div>
								  <div class="am-form-group">
								<label for="user-name" class="am-u-sm-3 am-form-label">简介</label>
								<div class="am-u-sm-9 am-u-md-9">
									<textarea name="introduce" id="introduce" ROWs="10" COLs="15">${introduce}</textarea><br>
								</div>
								</div>
								<div class="am-form-group">
									<label for="user-name" class="am-u-sm-3 am-form-label">归属区域</label>
								     <div class="am-u-sm-3 am-u-lg-3 am-text-left">
									<input type="text" name="province" id="province" value="${user.province.name }"  readonly="true"/>
								</div>
								<div class="am-u-sm-3 am-u-lg-3 am-text-left">
									<input type="text" name="city" id="city" value="${user.city.name }"  readonly="true"/>
								</div>
								<div class="am-u-sm-3 am-u-lg-3 am-text-left">
								<input type="text" name="county" id="county" value="${user.county.name }"  readonly="true"/>
								</div>
							   </div>
								  <div class="am-form-group">
									<div id="div_status">
									<label for="user-name" class="am-u-sm-3 am-form-label">状态</label>
									<div class="am-u-sm-9 am-u-md-9 am-text-right">
										<select class="selector1" id="status" name="status">
											<option value="">请选择状态</option>
											<option value="1">可用</option>
											<option value="0">禁用</option>
										</select>

									</div>
								</div>
								</div>
							</div>
						</div>
						 <div class="am-margin am-text-right">
								<button type="button" id="clear_button"
									class="am-btn am-btn-primary am-btn-xs">清空</button>
								<button type="button" id="search_button"
									class="am-btn am-btn-primary am-btn-xs">查询</button>
								<button type="button" id="save_button"
									class="am-btn am-btn-primary am-btn-xs">修改</button>
								<button type="button" id="add_button"
									class="am-btn am-btn-primary am-btn-xs">新增</button>
									<button type="button" id="delete_button"
								class="am-btn am-btn-primary am-btn-xs">删除</button>
						</div>
						
					</div>
				</div>
			</form>
			<div class="am-g">
				<div class="am-u-sm-12">
					<form class="am-form" >
						<table id="clients_table"
							class="am-table am-table-striped am-table-hover table-main">
							<thead>
								<tr>
								    <th class="table-title" width="20%">店铺名称</th>
									<th class="table-title" width="20%">店长名称</th>
									<th class="table-title" width="20%">电话</th>
									<th class="table-title" width="10%">经营类别</th>
									<th class="table-title" width="10%">查看位置</th>
									<th class="table-title" width="10%">状态</th>
									<th class="table-title" width="10%">关联工单</th>
								</tr>
							</thead>
							<tbody id="data_tbody">
								<c:forEach var="list" items="${storelist}" varStatus="s">
									<tr class="tr_click" store_id="${list.id}">
								
										<td>${list.shopname}</td>
										<td>${list.shopmanagename}</td>
										<td>${list.phone}</td>
										<td>${list.type}</td>
										<td>
										<c:choose>
												<c:when test="${list.businesslist.is_relation==1}">  
         											<a href="views/business/get_map.jsp?x=${list.businesslist.address_x}&y=${list.businesslist.address_y}&shopname=${list.businesslist.shopname}" target="_blank">	查看位置 </a>
   												</c:when>
												<c:otherwise> 
													   
  											    </c:otherwise>
											    </c:choose>
											</td>
											 <td class="td2"><c:choose>
												<c:when test="${list.status==1}">  
         											  可用     
   												</c:when>
												<c:otherwise> 
													   禁用 
  											    </c:otherwise>
											</c:choose>
											</td>
								 <td class="td_click"><input type="button"  class="am-btn am-btn-primary am-active am-btn-xs" id="${list.id}" value="关联工单"></td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
						 
						<div class="am-cf">
							共 <b id="pageCount">${page.allRow}</b> 条记录
							<div class="am-fr">
								<ul class="am-pagination">
									<c:choose>
										<c:when test="${page.currentPage==1}">
											<li class="am-disabled"><a href="#">«</a></li>
										</c:when>
										<c:when test="${page.currentPage!=1}">
											<li><a href="javascript:void(0)"
												onclick="choose_page(${page.currentPage-1})">«</a></li>
										</c:when>
									</c:choose>
									<c:forEach var="i" begin="1" end="${page.totalPage}" step="1">
										<c:choose>
											<c:when test="${page.currentPage==i}">
												<li class="am-active"><a href="javascript:void(0)"><c:out
															value="${i}" /></a></li>
											</c:when>
											<c:when test="${page.currentPage!=i}">
												<li><a href="javascript:void(0)"
													onclick="choose_page(${i})"><c:out value="${i}" /></a></li>
											</c:when>
										</c:choose>
									</c:forEach>
									<c:choose>
										<c:when test="${page.currentPage==page.totalPage}">
											<li class="am-disabled"><a href="#">»</a></li>
										</c:when>
										<c:when test="${page.currentPage!=page.totalPage}">
											<li><a href="javascript:void(0)"
												onclick="choose_page(${page.currentPage+1})">»</a></li>
										</c:when>
									</c:choose>
								</ul>
							</div>
						</div>
						
					</form>
				</div>
			</div>
		</div>

		<!-- content end -->

	</div>

	<a href="#" class="am-show-sm-only admin-menu"
		data-am-offcanvas="{target: '#admin-offcanvas'}"> <span
		class="am-icon-btn am-icon-th-list"></span>
	</a>

	<jsp:include page="/manage_foot.do" flush="true" />

	<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->

	<!--[if (gte IE 9)|!(IE)]><!-->
	<script src="assets/js/jquery.min.js"></script>
	<!--<![endif]-->
	<script src="assets/js/amazeui.min.js"></script>
	<script src="assets/js/app.js"></script>
	<script type="text/javascript">
	 function geturl(url){
	    	$("#filetext").val(url);
	    	 $("#imgdiv").show();
	    	 var r= new FileReader();
			   f=document.getElementById('file').files[0];
			   r.readAsDataURL(f);
			   r.onload=function  (e) {
			   document.getElementById('img1').src=this.result;
			   };
	    }
			

$(function() {
	//页面初始化时默认新增机构，所以隐藏新增按钮
//	$("#formdiv").hide();//隐藏
	$("#delete_button").hide();
	  $("#file").hide();
	 $("#div_status").hide();
	 $("#imgdiv").hide();
	 $("#save_button").hide();
	//保存按钮点击事件
	$("#save_button").click(function() {
		 $("#formdiv").show();
		 var formData = new FormData($( "#store_form" )[0]); 
		$.ajax({
			contentType:"application/json",
		    type : "POST",
		    url : "<%=request.getContextPath()%>/add_store.do?id="+$("#id").val()+"&status="+$("#status>option:selected").val()+"&province_code="+$("#province1").val()+"&city_code="+$("#city1").val()+"&county_code="+$("#county1").val()+"",
		    data :formData,
		    async: false,  
	          cache: false,  
	          contentType: false,  
	          processData: false,
		    success : function(data) {
		        if(data.flag=="1"){
		        	alert("添加成功")
		        	$("#store_form")[0].reset();
		       
		            $("#store_form").submit();
		        	$("#id").val(data.id);
		        	
		        }
		        else if(data.flag=="0"){
		            alert("保存失败！");
		        }else if(data.flag=="3")
		        	{
		        	  alert("图片上传失败！");
		        	}
		    },
		    error :function(){
		        alert("网络连接出错！");
		    }
		});
	});
	 
	$(".td_click").bind("click",function(){
		 $(".td_click").unbind("click");
		 $(".tr_click").unbind("click");
		location.href="<%=request.getContextPath()%>/show_businessbyname.do?store_id="+$(this).parent().attr("store_id")+"";
	});
	
	 $(".tr_click").bind("click",function(){
		 $.ajax({
				contentType:"application/json",
			    type : "GET",
			    url : "<%=request.getContextPath()%>/get_store.do?id="+$(this).attr("store_id"),
			    dataType: "json",
			    success : function(data) {
			        if(data.flag=="1"){
			        	  if(data.store.status==1)
		            	   {
		            	   $("#status option[value='1']").attr("selected","selected");
		            	   $("#status option[value='0']").removeAttr("selected");
		            	   }
		               if(data.store.status==0)
		            	   {
		            	   $("#status option[value='0']").attr("selected","selected");
		            	   $("#status option[value='1']").removeAttr("selected");
		            	   }
			        	$("#shopname").val(data.store.shopname);
			        	$("#shopmanagename").val(data.store.shopmanagename);
			        	$("#phone").val(data.store.phone);
			        	 $("#filetext").val(data.store.tradeimg_url);
			        	 $("#type").val(data.store.type);
			             $("#img1").attr("src","/"+data.store.tradeimg_url+"");
			        	$("#id").val(data.store.id);
			        	$("#introduce").val(data.store.introduce);
			        	
			        	var province_code = data.store.province.code;
			        	var city_code = data.store.city.code;
			        	var county_code = data.store.county.code;
			        	//重新加载省，并且选中
				   		 $.ajax({
								contentType:"application/json",
							    type : "POST",
							    url : "<%=request.getContextPath()%>/get_province.do", 
							    dataType: "json",
						    	success : function(data) {
							        if(data.flag=="1"){
								    	var length=data.area.length;
								        if(length>0){
							   		        var selDom=$("#province");
							   		   		selDom.html("");
										    for(var i=0;i<length;i++){										    	
										    	if(data.area[i].code == province_code){
										    		selDom.append("<option value='"+data.area[i].code+"' selectes='selected'>"+data.area[i].name+"</option>");	

										    	}else{
										    		selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
										    	}
										    } 
							    		}
							        }
						       }
						});
			        	
			        	//重新加载对应的市，并选中
			        	$.ajax({
							contentType:"application/json",
						    type : "POST",
						    url : "<%=request.getContextPath()%>/get_city/"+province_code+"/.do",
						    dataType: "json",
						    success : function(data) {
						        if(data.flag=="1"){				
							    	var length=data.area.length;
							       	if(length>0){
						   		   		var selDom=$("#city");
						   		        selDom.html("");			   		     
								    	for(var i=0;i<length;i++){
								    		if(data.area[i].code == city_code){
									    		selDom.append("<option value='"+data.area[i].code+"' selected='selected'>"+data.area[i].name+"</option>"); 
								    		}else{
									    		selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
								    		}
								    	}  
							      	}
						       }
						    }
						});
			        
			        	//重新加载对应的县，并选中
			        	$.ajax({
							contentType:"application/json",
							type : "POST",
							url : "<%=request.getContextPath()%>/get_county/"+city_code+"/.do", 
							dataType: "json",
							success : function(data) {
								if(data.flag=="1"){
							    	var length=data.area.length;
							    	console.log(length);
							        if(length>0){
					   		        	var selDom=$("#county");
					   		      		selDom.html("");
							    	 	for(var i=0;i<length;i++){
							    			if(data.area[i].code == county_code){
							    				selDom.append("<option value='"+data.area[i].code+"' selected='selected'>"+data.area[i].name+"</option>");									    		 
							    			}else{
							    				selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");		
							    			}
							    		 }  
							    	 }}
							       }
						});

			        }
			        else{
			            alert("获取失败！");
			        }
			    },
			    error :function(){
			        alert("网络连接出错！");
			    }
			});
		 $(".tr_click").removeClass("am-active");
		 $(this).addClass("am-active");
         $("#save_button").show();//显示
         $("#delete_button").show();
         $("#div_status").show();
         $("#imgdiv").show();
	 });
	 //清空按钮点击事件
	 $("#clear_button").click(function(){
		 $(".tr_click").removeClass("am-active");
		 $("#id").val(0);
		 $('#store_form')[0].reset();
		 $("#shopname").val('');
		 $("#shopmanagename").val('');
		
		 $("#type").val('');
		 $("#phone").val("");
		 $("#imgdiv").hide();
		 $("#div_status").hide();
		 $("#introduce").val("");
         $("#save_button").hide();//隐藏
	 });
	 
	//新增按钮点击事件
	 $("#add_button").click(function(){
		 $("#id").val(0);
		 $("#save_button").click();
		
	 });
	
 	 $("#delete_button").click(function(){
 		
		 $.ajax({
				contentType:"application/json",
			    type : "GET",
			    url : "<%=request.getContextPath()%>/delete_store.do?id="+$("#id").val()+"",
			    dataType: "json",
			    success : function(data) {
			        if(data.flag=="1"){
			        	 alert("删除成功！");
			        	 $("#id").val(0);
			    		 $('#clear_button').click();
			    		 $("#store_form").submit();
			        }
			        else{
			            alert("删除失败！");
			        }
			    },
			    error :function(){
			        alert("网络连接出错！");
			    }
			});
		 $(".tr_click").removeClass("am-active");
		 $(this).addClass("am-active");
         $("#save_button").show();//显示
	 });
	 
 	 $("#search_button").click(function(){
		 $("#store_form").submit();
	 });
});

function choose_page(i){
	 $("#clear_button").click();
	$("#page_num").val(i);
	 $("#status").val('');
	$("#store_form").submit();
}
</script>
</body>
</html>
